/*********************************************************************************
 *      Copyright:  (C) 2024 linuxer<linuxer@email.com>
 *                  All rights reserved.
 *
 *       Filename:  sqlite.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(15/03/24)
 *         Author:  linuxer <linuxer@email.com>
 *      ChangeLog:  1, Release initial version on "15/03/24 18:55:10"
 *                 
 ********************************************************************************/
#include"database.h"


static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
	int i;
	for(i=0; i<argc; i++)
	{
		printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
	}
	printf("\n");
	return 0;
}
//创建数据库
void create_db(sqlite3* db,char* db_name)
{
	char*		zErrMsg=0;

	//创建数据库
	int rc=sqlite3_open(db_name,&db);
	if( rc )
	{
		 fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
		 exit(0);
	}
	else
	{
		fprintf(stderr, "Opened database successfully\n");
	}
	sqlite3_close(db);
}

//创建表
void  create_db_table(sqlite3* db,char* db_name,char *table_name)
{
	char 		sql[128]={0};
	char*		zErrMsg=0;	

	int rc=sqlite3_open(db_name,&db);
	if( rc )
	{
		 log_error("Can't open database: %s\n", sqlite3_errmsg(db));
		 exit(0);
	}
	else
	{
		log_debug("Opened database successfully\n");
	}

	sprintf(sql,"CREATE TABLE %s("
	    "serial 				TEXT     NOT NULL," 
		"temperature           	TEXT    NOT NULL," 
		"time		            TEXT     NOT NULL"");",table_name);
	//sfprintf(sql,"create table %s(serial Char,temperature Char,time Char);",table_name);

	rc=sqlite3_exec(db,sql,callback,0,&zErrMsg);
	if( rc != SQLITE_OK )
	{
		log_error("SQL error: %s\n",zErrMsg);
		sqlite3_free(zErrMsg);
	}
	else
	{
		log_info("Table created successfully\n");
	}
	sqlite3_close(db);
}

void tb_add(sqlite3* db,char* db_name,char* table_name,char *serial,char* temperature,char *time)
{
	char*	zErrMsg=0;
	char	sql[128]={0};

	int rc=sqlite3_open(db_name,&db);
	if( rc!=SQLITE_OK )
	{
		 log_error("Can't open database: %s\n", sqlite3_errmsg(db));
		 exit(0);
	}
	else
	{
		log_debug("Opened database successfully\n");
	}

	sprintf(sql,"insert into %s values('%s','%s','%s');",table_name,serial,temperature,time);


	rc=sqlite3_exec(db,sql,callback,0,&zErrMsg);
	if( rc != SQLITE_OK )
	{
		log_error("SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
	}
	else
	{
		log_debug("Records add successfully\n");
	}
	sqlite3_close(db);

}

//根据时间删除
void tb_delete_temp(sqlite3* db,char* db_name,char* table_name,char *time)
{
	char*	zErrMsg=0;
	char	sql[128]={0};

	int rc=sqlite3_open(db_name,&db);
	if( rc )
	{
		 log_error("Can't open database: %s\n", sqlite3_errmsg(db));
		 exit(0);
	}
	else
	{
		log_debug("Opened database successfully\n");
	}

	sprintf(sql,"delete from %s where time=%s;",table_name,time);

	rc=sqlite3_exec(db,sql,callback,0,&zErrMsg);
	if( rc != SQLITE_OK )
	{
		log_error("SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
	}
	else
	{
		log_debug("Operation done successfully\n");
	}
	sqlite3_close(db);
}
//选择所有数据
void tb_select(sqlite3* db,char* db_name,char* table_name)
{
	char*	zErrMsg=0;
	char	sql[128]={0};

	int rc=sqlite3_open(db_name,&db);
	if( rc )
	{
		 log_error("Can't open database: %s\n", sqlite3_errmsg(db));
		 exit(0);
	}
	else
	{
		log_debug("Opened database successfully\n");
	}

	sprintf(sql,"select *from %s;",table_name);


	rc=sqlite3_exec(db,sql,callback,0,&zErrMsg);
	if( rc != SQLITE_OK )
	{
		log_error("SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
	}
	else
	{
		log_debug("Operation done successfully\n");
	}
}

